<script setup lang="tsx">
import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { fetchDeleteVPNTemplate, fetchGetVPNTemplateList } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import {
  // dpdEnableRecord,
  encapsulationModeRecord,
  // ipsecAuthAlgorithmRecord,
  ikeVerifyMethodRecord,
  // ipsecTransformRecord,
  ipsecTypeRecord
} from '@/constants/business';
import { useTable, useTableOperate } from '@/hooks/common/table';
import TemplateOperateDrawer from './modules/template-operate-drawer.vue';
import TemplateSearch from './modules/template-search.vue';

const appStore = useAppStore();

const {
  columns,
  columnChecks,
  data,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetVPNTemplateList,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    name: null
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'index',
      title: '序号',
      align: 'center',
      width: 64
    },
    {
      key: 'name',
      title: '模板名称',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'encapsulationMode',
      title: '封包模式',
      align: 'center',
      minWidth: 100,
      render: row => {
        if (row.encapsulationMode === null) {
          return null;
        }
        const label = encapsulationModeRecord.get(row.encapsulationMode);

        return label;
      }
    },
    {
      key: 'ipsecType',
      title: '类型',
      align: 'center',
      minWidth: 100,
      render: row => {
        if (row.ipsecType === null) {
          return null;
        }
        const label = ipsecTypeRecord.get(row.ipsecType);

        return label;
      }
    },
    // {
    //   key: 'ipsecAuthAlgorithm',
    //   title: 'IPSec 认证算法',
    //   align: 'center',
    //   minWidth: 100,
    //   render: row => {
    //     if (row.ipsecAuthAlgorithm === null) {
    //       return null;
    //     }
    //     const label = ipsecAuthAlgorithmRecord.get(row.ipsecAuthAlgorithm);

    //     return label;
    //   }
    // },
    {
      key: 'ikeVerifyMethod',
      title: '认证方式',
      align: 'center',
      minWidth: 100,
      render: row => {
        if (row.ipsecAuthAlgorithm === null) {
          return null;
        }
        const label = ikeVerifyMethodRecord.get(row.ikeVerifyMethod);

        return label;
      }
    },
    {
      key: 'ipsecAuthAlgorithm',
      title: '是否启用量子密钥',
      align: 'center',
      minWidth: 100,
      render: () => <NTag type="success">是</NTag>
    },
    // {
    //   key: 'ipsecTransform',
    //   title: 'IPSec 封包协议',
    //   align: 'center',
    //   minWidth: 100,
    //   render: row => {
    //     if (row.ipsecTransform === null) {
    //       return null;
    //     }

    //     const label = ipsecTransformRecord.get(row.ipsecTransform);

    //     return label;
    //   }
    // },
    // {
    //   key: 'dpdEnable',
    //   title: 'DPD探测是否启用',
    //   align: 'center',
    //   minWidth: 100,
    //   render: row => {
    //     if (row.dpdEnable === null) {
    //       return null;
    //     }
    //     const tagMap: Map<Api.Common.EnableStatus, NaiveUI.ThemeColor> = new Map([
    //       [true, 'success'],
    //       [false, 'warning']
    //     ]);
    //     const label = dpdEnableRecord.get(row.dpdEnable);

    //     return <NTag type={tagMap.get(row.dpdEnable)}>{label}</NTag>;
    //   }
    // },
    {
      key: 'operate',
      title: '操作',
      align: 'center',
      width: 130,
      render: row => (
        <div class="flex-center gap-8px">
          <NButton type="primary" ghost size="small" onClick={() => edit(row)}>
            编辑
          </NButton>
          <NPopconfirm onPositiveClick={() => handleDelete(row.id!)}>
            {{
              default: () => '确认删除吗',
              trigger: () => (
                <NButton type="error" ghost size="small">
                  删除
                </NButton>
              )
            }}
          </NPopconfirm>
        </div>
      )
    }
  ]
});

const { drawerVisible, operateType, editingData, handleAdd, handleEdit, checkedRowKeys, onBatchDeleted, onDeleted } =
  useTableOperate(data, getData);

async function handleBatchDelete() {
  const { error } = await fetchDeleteVPNTemplate(
    checkedRowKeys.value as TicketManageApi.VPNTemplateManage.DeleteTemplateParams
  );
  if (error) return;
  onBatchDeleted();
}

async function handleDelete(id: number) {
  const { error } = await fetchDeleteVPNTemplate([id]);
  if (error) return;
  onDeleted();
}

function edit(row: NaiveUI.TableDataWithIndex<TicketManageApi.VPNTemplateManage.Template>) {
  handleEdit(row);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <TemplateSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <NCard title="量子模板列表" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          add-button
          @add="handleAdd"
          @delete="handleBatchDelete"
          @refresh="getData"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="962"
        :loading="loading"
        remote
        :row-key="row => row.id"
        :pagination="mobilePagination"
        class="sm:h-full"
      />
      <TemplateOperateDrawer
        v-model:visible="drawerVisible"
        :operate-type="operateType"
        :row-data="editingData"
        @submitted="getDataByPage(searchParams.current || 1)"
      />
    </NCard>
  </div>
</template>

<style scoped></style>
